//
//  YiDatabase+Api.swift
//  yiplayer
//
//  Created by wkun on 2024/6/13.
//

import Foundation

extension YiDatabase{
//    let sqlServer = "name text, ip text, path text,username text, pwd text, type Integer, imgname text, note text".sql(tableServer)
    
    func addServer(name: String, ip: String, path: String, username: String, pwd: String, type: Int, imgname: String, note: String) -> Bool {
        let sql = "INSERT INTO \(tableServer) (name, ip, path, username, pwd, type, imgname, note) VALUES ('\(name)', '\(ip)','\(path)','\(username)','\(pwd)','\(type)','\(imgname)','\(note)')"
        return self.executeUpdate(sql: sql)
    }
    
    func deleteServer(_ serverId:String?) -> Bool {
        return self.deleteData(ID: serverId, dataBase: self.db, tableName: tableServer)
    }
    
    func modify() {
        
    }
    
    func getServerList() -> [YiServerListCellModel]? {
        let sql = "SELECT * FROM \(tableServer) order by id ASC"
        if let r = self.getDatas(sql: sql, dataBase: self.db) {
            var arr = [YiServerListCellModel]()
            while( r.next() ) {
//                name, ip, path, username, pwd, type, imgname, note
                let m = YiServerListCellModel()
                m.serverId = r.string(forColumn: "id")
                m.text = r.string(forColumn: "name")
                m.ip = r.string(forColumn: "ip")
                m.path = r.string(forColumn: "path")
                m.username = r.string(forColumn: "username")
                m.pwd = r.string(forColumn: "pwd")
                let i = r.int(forColumn: "type")
                m.type = ServerType.init(rawValue: Int(i))
                m.imgname = r.string(forColumn: "imgname")
                m.note = r.string(forColumn: "note")
                
                //m.createTime = r.string(forColumn: "createTime")
                arr.append(m)
            }
            return arr
        }
        return nil
    }
}
